//
//  LagouView.m
//  作业2
//
//  Created by dllo on 16/7/16.
//  Copyright © 2016年 YMJ. All rights reserved.
//

#import "LagouView.h"

#import "ViewController.h"
#import "RegisterViewController.h"
@implementation LagouView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
+ (LagouView *)createViewWithImageName1:(NSString *)imageName1
                             imageName2:(NSString *)imageName2
                                string1:(NSString *)string1
                                string2:(NSString *)string2
                         identifyString:(NSString *)identifyString
                              viewframe:(CGRect)viewframe
                                 center:(CGPoint)center
                          leftViewColor:(UIColor *)leftViewColor
               textFieldBackgroundColor:(UIColor *)textFieldBackgroundColor
                             cursorMove:(BOOL)cursorMove {
    
    LagouView *inputView = [[LagouView alloc] initWithFrame:viewframe];
    inputView.center = center;
    // CGRectMake(0, 0, WIDTH - 20, 100)
    // CGPointMake(WIDTH / 2, HEIGHT / 2)
    // 继承TextField的自定义类
    [inputView setTag:1000];
 
    inputView.text1 = [[CustomTextField alloc] initWithFrame:CGRectMake(0, 8, inputView.frame.size.width, inputView.frame.size.height / 2 - 9)];
    inputView.text1.backgroundColor = [textFieldBackgroundColor colorWithAlphaComponent:0.4];
    [inputView addSubview:inputView.text1];
    inputView.text2 = [[CustomTextField alloc] initWithFrame:CGRectMake(0, inputView.frame.size.height / 2 +2, inputView.frame.size.width, inputView.frame.size.height / 2 - 9)];
    inputView.text2.backgroundColor = [textFieldBackgroundColor colorWithAlphaComponent:0.4];
    // [UIColor darkGrayColor]
    [inputView addSubview:inputView.text2];
    //
    inputView.text1.placeholder = string1;
    inputView.text2.placeholder = string2;
//    [inputView.text1 setTextColor:textColor];
    [inputView.text1 setFont:[UIFont boldSystemFontOfSize:18]];
//    [inputView.text2 setTextColor:textColor];
    [inputView.text2 setFont:[UIFont boldSystemFontOfSize:18]];
    // 选择光标向右边移动
    if (cursorMove) {
        //左边视图1
        UIView *leftView1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 60, 40)];
        leftView1.backgroundColor = [UIColor clearColor];
        UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 45, 40)];
        view1.backgroundColor = leftViewColor;
        [leftView1 addSubview:view1];
        inputView.text1.imageView1 = [[UIImageView alloc]initWithFrame:CGRectMake(15, 0, 25, 25)];
        inputView.text1.imageView1.center = CGPointMake(view1.center.x, view1.center.y);
        [inputView.text1.imageView1 setImage:[UIImage imageNamed:imageName1]];
        [view1 addSubview:inputView.text1.imageView1];
        // 添加左边视图1
        inputView.text1.leftView = leftView1;
        inputView.text1.leftViewMode = UITextFieldViewModeAlways;
        //左边视图2
        UIView *leftView2 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 60, 40)];
        leftView2.backgroundColor = [UIColor clearColor];
        UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 45, 40)];
        view2.backgroundColor = leftViewColor;
        [leftView2 addSubview:view2];
        inputView.text2.imageView2 = [[UIImageView alloc]initWithFrame:CGRectMake(15, 0, 25, 25)];
        inputView.text2.imageView2.center = CGPointMake(view2.center.x, view2.center.y);
        [inputView.text2.imageView2 setImage:[UIImage imageNamed:imageName2]];
        // 添加左边视图2
        [view2 addSubview:inputView.text2.imageView2];
        inputView.text2.leftView = leftView2;
        inputView.text2.leftViewMode = UITextFieldViewModeAlways;

    } else {
        UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 45, 40)];
        view1.backgroundColor = leftViewColor;
        // [UIColor blackColor]
        inputView.text1.imageView1 = [[UIImageView alloc]initWithFrame:CGRectMake(15, 0, 25, 25)];
        inputView.text1.imageView1.center = CGPointMake(view1.center.x, view1.center.y);
        [inputView.text1.imageView1 setImage:[UIImage imageNamed:imageName1]];
        [view1 addSubview:inputView.text1.imageView1];
        inputView.text1.leftView = view1;
        inputView.text1.leftViewMode = UITextFieldViewModeAlways;
        //
        UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 45, 40)];
        view2.backgroundColor = leftViewColor;
        inputView.text2.imageView2 = [[UIImageView alloc]initWithFrame:CGRectMake(15, 0, 25, 25)];
        inputView.text2.imageView2.center = CGPointMake(view2.center.x, view2.center.y);
        [inputView.text2.imageView2 setImage:[UIImage imageNamed:imageName2]];
        [view2 addSubview:inputView.text2.imageView2];
        inputView.text2.leftView = view2;
        inputView.text2.leftViewMode = UITextFieldViewModeAlways;
        
    }
        // 验证码button
    if ([identifyString isEqualToString:@"获取验证码"]) {
        inputView.identifyButton = [LagouButton createButtonWithTitle:identifyString backgroundColor:nil titleColor:COLOR1 center:CGPointMake(25, 10) frame:CGRectMake(0, 0, 140, 50) borderWidth:2 cornerRadius:0 borderColor:nil];
//        inputView.identifyButton.frame = CGRectMake(0, 0, 140, 50);
        inputView.text2.rightView = inputView.identifyButton;
        inputView.text2.rightViewMode = UITextFieldViewModeAlways;

    }
    // 签订协议
    inputView.text1.delegate = inputView;
    inputView.text2.delegate = inputView;
    // 改变光标颜色
    [[UITextField appearance] setTintColor:COLOR1];
    // 改变初始光标位置
    
 
    return inputView;
}
+ (LagouView *)createViewWithString1:(NSString *)string1
                          string2:(NSString *)string2
                          string3:(NSString *)string3 {
    LagouView *view = [[LagouView alloc] initWithFrame:CGRectMake(0, 0, WIDTH - 20, HEIGHT / 3)];
    view.center = CGPointMake(WIDTH / 2, HEIGHT / 4 * 3 - 16);
    view.backgroundColor = [UIColor clearColor];
    // 忘记密码
    
    if ([string1 isEqualToString:@"忘记密码"]) {
        view.infoHelpButton = [LagouButton createButtonWithTitle:string1 backgroundColor:[UIColor clearColor] titleColor:[UIColor whiteColor] center:CGPointMake(WIDTH - 60, 20) frame:CGRectMake(0, 0, 120, 40) borderWidth:0 cornerRadius:0 borderColor:nil];
        
        UIImageView *infoHelpImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_infoHelp@3x.png"]];
        infoHelpImageView.frame = CGRectMake(WIDTH - 120 - 20, 10, 20, 20);
        [view addSubview:infoHelpImageView];
        
    } else if ([string1 isEqualToString:@"拉勾用户协议"]) {
        view.infoHelpButton = [LagouButton createButtonWithTitle:string1 backgroundColor:[UIColor clearColor] titleColor:COLOR1 center:CGPointMake(WIDTH - 100, 20) frame:CGRectMake(0, 0, 150, 40) borderWidth:0 cornerRadius:0 borderColor:nil];
        UIImageView *infoHelpImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_delegate@#3x.png"]];
        infoHelpImageView.frame = CGRectMake(50, 10, 20, 20);
        [view addSubview:infoHelpImageView];
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(80, 10, 190, 20)];
        label.backgroundColor = [UIColor clearColor];
        label.alpha = 0.8;
        label.text = @"注册代表你已同意";
        [label setTextColor:[UIColor whiteColor]];
        label.font = [UIFont boldSystemFontOfSize:20];
        [view addSubview:label];
    }
    [view addSubview:view.infoHelpButton];
    
   
    // 登录button
    view.signinButton = [LagouButton createButtonWithTitle:string2 backgroundColor:COLOR1 titleColor:[UIColor whiteColor] center:CGPointMake((WIDTH - 20) / 2, 100) frame:CGRectMake(0, 0, WIDTH - 20, 50) borderWidth:0 cornerRadius:5 borderColor:nil];
    [view addSubview:view.signinButton];
    // CGRectMake(0, 0, WIDTH - 20, 50)
    // 注册拉勾
    view.registerButton= [LagouButton createButtonWithTitle:string3 backgroundColor:COLOR2 titleColor:COLOR1 center:CGPointMake((WIDTH - 20) / 2, 180) frame:CGRectMake(0, 0, WIDTH - 20, 50) borderWidth:2 cornerRadius:0 borderColor:COLOR1.CGColor];
    [view addSubview:view.registerButton];

    return view;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    NSLog(@"开始编辑");
    LagouView *inputView = [self viewWithTag:1000];
    // 选中某textField的图片变化

    if ([textField.placeholder isEqualToString:@"请输入已验证的手机号或邮箱"] || [textField.placeholder isEqualToString:@"请输入密码"]) {
        if ([textField isEqual:inputView.text1]) {
            
            [inputView.text1.imageView1 setImage:[UIImage imageNamed:@"icon_person1@3x.png"]];
            [inputView.text2.imageView2 setImage:[UIImage imageNamed:@"icon_password@3x.png"]];
        } else if ([textField isEqual:inputView.text2]) {
            [inputView.text2.imageView2 setImage:[UIImage imageNamed:@"icon_password1@3x.png"]];
            [inputView.text1.imageView1 setImage:[UIImage imageNamed:@"icon_person@3x.png"]];
            
        }
    } else if ([textField.placeholder isEqualToString:@"请输入手机号"] || [textField.placeholder isEqualToString:@"请输入验证码"]) {
        if ([textField isEqual:inputView.text1]) {
            
            [inputView.text1.imageView1 setImage:[UIImage imageNamed:@"icon_phone@3x.png"]];
            [inputView.text2.imageView2 setImage:[UIImage imageNamed:@"icon_identify1@3x.png"]];
        } else if ([textField isEqual:inputView.text2]) {
            [inputView.text2.imageView2 setImage:[UIImage imageNamed:@"icon_identify@3x.png"]];
            [inputView.text1.imageView1 setImage:[UIImage imageNamed:@"icon_iphone1@3x.png"]];
            
        }
    }
    // 开始编辑时textField向上移动(封装一个textField)
//    CGFloat h = textField.center.y - HEIGHT / 2;
//    if (h < 100) {
//        ViewController *vc = [[ViewController alloc] init];
//        LagouImageView *backgroundImageView = [vc.view viewWithTag:1001];
//        [backgroundImageView setCenter:CGPointMake(WIDTH / 2, 0)];
//        NSLog(@"是是是");
//        NSLog(@"%f",backgroundImageView.center.y);
//    }
 
    return YES;
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
    NSLog(@"结束编辑");
    return YES;
}

@end
